/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package HumanCentredSystemDesign;

import java.util.Date;

/**
 *
 * @author Martin and Chris
 */
public class ReservationManager extends LibraryCirculations {
    public Item myItem;
    
    public ReservationManager(Item _myItem, Borrower _myBorrower ){
        myBorrower = _myBorrower;
        myItem = _myItem ;
        
    }
    
    public Reservation Reserve(){
 
        if(alreadyReserved() == true){
            AlertWindow aw = new AlertWindow("You have already reserved this Item");
            aw.setVisible(true);
            return null;
        }
        else {    
            if (checkFreeCopies() == true){
                
                AlertWindow aw = new AlertWindow("There are free Copies available to loan");
                aw.setVisible(true);
                
            }else{
                Loan earliestIssuedLoan = new Loan();
                Date today = new Date();
                earliestIssuedLoan.setIssueDate(today);
                for ( Copy copy : myItem.getItemCopies()){

                       if (copy.onLoan())
                       { 
                            copy.getLoanDetails().getIssueDate();
                            //Recall the copy with the earliestDate
                            if( copy.getLoanDetails().getIssueDate().before(earliestIssuedLoan.getIssueDate()))
                            {
                                earliestIssuedLoan = copy.getLoanDetails();
                            }
                            else{}
                        }
                }
                int period = earliestIssuedLoan.getLoanPeriod();
                Date issued = earliestIssuedLoan.getIssueDate();
                Date recallDue = new Date();
                recallDue.setDate(recallDue.getDate()+7);
                Date recallCheck = new Date();
                recallCheck.setDate(recallCheck.getDate()+7-period);
                if(recallCheck.before(issued)){
                    long periodMs = recallDue.getTime() - issued.getTime();
                    int periodSec = (int) (periodMs/1000);
                    int periodMin = periodSec/60;
                    int periodHour = periodMin/60;
                    int periodDay = periodHour/24;
                    period = periodDay;
                    
                    earliestIssuedLoan.setLoanPeriod(period);
                    earliestIssuedLoan.recall();
                }
                AlertWindow aw = new AlertWindow("We have recalled the earliest issued copy of this item </br> Please wait 7 days. </br> The Item '" + myItem.getTitle() + "' has been reserved.");
                aw.setVisible(true);
                //Create a reservation
            }
            
            Reservation myReservation = new Reservation();
            myReservation.setItemIdentifier(myItem.itemIdentifier);
            myReservation.setLibraryMemberID(myBorrower.getBorrowerID());
            myReservation.setReservationDate(new Date());
            myReservation.addReservationToDatabase();
            return myReservation;
  
            
        }
        
    }
    
    
    public boolean alreadyReserved(){
        Connect myConn = new Connect();
        myConn.establishConnection();

        if (myConn.getReservationsFromDatabase(myItem.itemIdentifier, myBorrower.libraryMemberID).isEmpty())
        {
            myConn.closeConnection();
            return false;
        }
        else {
            myConn.closeConnection();
            return true;
        }
        
    }
        
    
    
    public boolean checkFreeCopies(){
        boolean freeCopies = false;
        for (Copy c : myItem.getItemCopies())
        {
            if(c.onLoan() == false  && c.isReferenceOnly() == false)
            {
                freeCopies = true;
            }
        }
        
        return freeCopies;
    }
    

    
}
